/*     */ package com.zimbra.cs.service.mail;
/*     */ 
/*     */ import com.zimbra.common.calendar.ZCalendar.ZVCalendar;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.mailbox.CalendarItem;
/*     */ import com.zimbra.cs.mailbox.CalendarItem.ReplyInfo;
/*     */ import com.zimbra.cs.mailbox.Flag;
/*     */ import com.zimbra.cs.mailbox.Folder;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.Mailbox.SetCalendarItemData;
/*     */ import com.zimbra.cs.mailbox.MailboxLock;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.cs.mailbox.calendar.Invite;
/*     */ import com.zimbra.cs.mailbox.calendar.RecurId;
/*     */ import com.zimbra.cs.mailbox.util.TagUtil;
/*     */ import com.zimbra.cs.mime.ParsedMessage;
/*     */ import com.zimbra.cs.mime.ParsedMessage.CalendarPartInfo;
/*     */ import com.zimbra.cs.service.util.ItemId;
/*     */ import com.zimbra.cs.service.util.ItemIdFormatter;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import javax.mail.internet.MimeMessage;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class SetCalendarItem
/*     */   extends CalendarRequest
/*     */ {
/*     */   public static class SetCalendarItemParseResult
/*     */   {
/*     */     public Mailbox.SetCalendarItemData defaultInv;
/*     */     public Mailbox.SetCalendarItemData[] exceptions;
/*     */     public List<CalendarItem.ReplyInfo> replies;
/*     */     public boolean isTodo;
/*     */     public long nextAlarm;
/*     */   }
/*     */   
/*  53 */   private static final String[] TARGET_FOLDER_PATH = { "l" };
/*     */   
/*     */   protected String[] getProxiedIdPath(Element request)
/*     */   {
/*  57 */     return TARGET_FOLDER_PATH;
/*     */   }
/*     */   
/*     */   protected boolean checkMountpointProxy(Element request)
/*     */   {
/*  62 */     return true;
/*     */   }
/*     */   
/*     */   static class SetCalendarItemInviteParser extends ParseMimeMessage.InviteParser
/*     */   {
/*  67 */     private boolean mExceptOk = false;
/*  68 */     private boolean mForCancel = false;
/*     */     private final Folder mFolder;
/*     */     private final MailItem.Type type;
/*     */     
/*     */     SetCalendarItemInviteParser(boolean exceptOk, boolean forCancel, Folder folder, MailItem.Type type) {
/*  73 */       this.mExceptOk = exceptOk;
/*  74 */       this.mForCancel = forCancel;
/*  75 */       this.mFolder = folder;
/*  76 */       this.type = type;
/*     */     }
/*     */     
/*     */     public ParseMimeMessage.InviteParserResult parseInviteElement(ZimbraSoapContext zc, OperationContext octxt, Account account, Element inviteElem)
/*     */       throws ServiceException
/*     */     {
/*  82 */       Element content = inviteElem.getOptionalElement("content");
/*  83 */       if (content != null) {
/*  84 */         ParseMimeMessage.InviteParserResult toRet = CalendarUtils.parseInviteRaw(account, inviteElem);
/*  85 */         return toRet;
/*     */       }
/*  87 */       if (this.mForCancel) {
/*  88 */         return CalendarUtils.parseInviteForCancel(account, this.mFolder, this.type, inviteElem, null, this.mExceptOk, true);
/*     */       }
/*     */       
/*  91 */       return CalendarUtils.parseInviteForCreate(account, this.type, inviteElem, null, null, this.mExceptOk, true);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public Element handle(Element request, Map<String, Object> context)
/*     */     throws ServiceException
/*     */   {
/* 100 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/* 101 */     Mailbox mbox = getRequestedMailbox(zsc);
/* 102 */     OperationContext octxt = getOperationContext(zsc, context);
/* 103 */     ItemIdFormatter ifmt = new ItemIdFormatter(zsc);
/*     */     
/* 105 */     int flags = Flag.toBitmask(request.getAttribute("f", null));
/* 106 */     String[] tags = TagUtil.parseTags(request, mbox, octxt);
/*     */     
/* 108 */     mbox.lock.lock();
/*     */     try {
/* 110 */       int defaultFolder = getItemType() == MailItem.Type.TASK ? 15 : 10;
/* 111 */       String defaultFolderStr = Integer.toString(defaultFolder);
/* 112 */       String folderIdStr = request.getAttribute("l", defaultFolderStr);
/* 113 */       ItemId iidFolder = new ItemId(folderIdStr, zsc);
/* 114 */       Folder folder = mbox.getFolderById(octxt, iidFolder.getId());
/* 115 */       SetCalendarItemParseResult parsed = parseSetAppointmentRequest(request, zsc, octxt, folder, getItemType(), false);
/*     */       
/* 117 */       CalendarItem calItem = mbox.setCalendarItem(octxt, iidFolder.getId(), flags, tags, parsed.defaultInv, parsed.exceptions, parsed.replies, parsed.nextAlarm);
/*     */       
/*     */ 
/*     */ 
/* 121 */       Element response = getResponseElement(zsc);
/*     */       
/* 123 */       if (parsed.defaultInv != null) {
/* 124 */         response.addElement("default").addAttribute("id", ifmt.formatItemId(parsed.defaultInv.invite.getMailItemId()));
/*     */       }
/*     */       
/*     */ 
/* 128 */       if (parsed.exceptions != null) {
/* 129 */         for (Mailbox.SetCalendarItemData cur : parsed.exceptions) {
/* 130 */           Element e = response.addElement("except");
/* 131 */           e.addAttribute("recurId", cur.invite.getRecurId().toString());
/* 132 */           e.addAttribute("id", ifmt.formatItemId(cur.invite.getMailItemId()));
/*     */         }
/*     */       }
/* 135 */       String itemId = ifmt.formatItemId(calItem == null ? 0 : calItem.getId());
/* 136 */       response.addAttribute("calItemId", itemId);
/*     */       try {
/* 138 */         Element inv = request.getElement("default").getElement("m").getElement("inv");
/* 139 */         Element comp = inv.getOptionalElement("comp");
/* 140 */         if (comp != null) {
/* 141 */           inv = comp;
/*     */         }
/* 143 */         String reqCalItemId = inv.getAttribute("calItemId");
/* 144 */         String uid = inv.getAttribute("uid");
/* 145 */         boolean uidSame = (calItem == null) || ((calItem.getUid() == null) && (uid == null)) || ((calItem.getUid() != null) && ((calItem.getUid().equals(uid)) || ((Invite.isOutlookUid(calItem.getUid())) && (calItem.getUid().equalsIgnoreCase(uid)))));
/* 146 */         if (ZimbraLog.calendar.isInfoEnabled()) {
/* 147 */           StringBuilder logBuf = new StringBuilder();
/* 148 */           if (!reqCalItemId.equals(itemId)) {
/* 149 */             logBuf.append("Mapped requested id ").append(reqCalItemId).append(" -> ").append(itemId);
/*     */           }
/* 151 */           if (!uidSame) {
/* 152 */             logBuf.append(" ?? requested UID ").append(uid).append(" differs from mapped ").append(calItem.getUid());
/* 153 */             ZimbraLog.calendar.warn(logBuf.toString());
/* 154 */           } else if (logBuf.length() > 0) {
/* 155 */             ZimbraLog.calendar.info(logBuf.toString());
/*     */           }
/*     */         }
/* 158 */         if ((!$assertionsDisabled) && (!uidSame)) { throw new AssertionError();
/*     */         }
/*     */       }
/*     */       catch (ServiceException se) {}
/* 162 */       if (!parsed.isTodo) {
/* 163 */         response.addAttribute("apptId", itemId);
/*     */       }
/* 165 */       return response;
/*     */     } finally {
/* 167 */       mbox.lock.release();
/*     */     }
/*     */   }
/*     */   
/*     */   static Mailbox.SetCalendarItemData getSetCalendarItemData(ZimbraSoapContext zsc, OperationContext octxt, Account acct, Mailbox mbox, Element e, ParseMimeMessage.InviteParser parser) throws ServiceException
/*     */   {
/* 173 */     String partStatStr = e.getAttribute("ptst", "NE");
/*     */     
/*     */ 
/* 176 */     Element msgElem = e.getElement("m");
/*     */     
/*     */ 
/* 179 */     String attachmentId = msgElem.getAttribute("aid", null);
/* 180 */     Element contentElement = msgElem.getOptionalElement("content");
/*     */     
/* 182 */     ParseMimeMessage.InviteParserResult ipr = null;
/*     */     
/* 184 */     MimeMessage mm = null;
/* 185 */     if (attachmentId != null) {
/* 186 */       ParseMimeMessage.MimeMessageData mimeData = new ParseMimeMessage.MimeMessageData();
/* 187 */       mm = SendMsg.parseUploadedMessage(zsc, attachmentId, mimeData);
/* 188 */     } else if (contentElement != null) {
/* 189 */       mm = ParseMimeMessage.importMsgSoap(msgElem);
/*     */     } else {
/* 191 */       CalendarRequest.CalSendData dat = handleMsgElement(zsc, octxt, msgElem, acct, mbox, parser);
/* 192 */       mm = dat.mMm;
/* 193 */       ipr = parser.getResult();
/*     */     }
/*     */     
/* 196 */     if ((ipr == null) && (msgElem.getOptionalElement("inv") != null)) {
/* 197 */       ipr = parser.parse(zsc, octxt, mbox.getAccount(), msgElem.getElement("inv"));
/*     */       
/*     */ 
/*     */ 
/* 201 */       if ((ipr != null) && (ipr.mInvite != null) && (mm != null)) {
/* 202 */         String desc = Invite.getDescription(mm, "text/plain");
/* 203 */         String descHtml = Invite.getDescription(mm, "text/html");
/* 204 */         if (((desc != null) && (desc.length() > 0)) || ((descHtml != null) && (descHtml.length() > 0))) {
/* 205 */           ipr.mInvite.setDescription(desc, descHtml);
/*     */         }
/*     */       }
/*     */     }
/*     */     
/* 210 */     ParsedMessage pm = new ParsedMessage(mm, mbox.attachmentsIndexingEnabled());
/*     */     
/* 212 */     Invite inv = ipr == null ? null : ipr.mInvite;
/* 213 */     if ((inv == null) || (inv.getDTStamp() == -1L)) {
/* 214 */       ParsedMessage.CalendarPartInfo cpi = pm.getCalendarPartInfo();
/* 215 */       ZCalendar.ZVCalendar cal = null;
/* 216 */       if ((cpi != null) && (CalendarItem.isAcceptableInvite(mbox.getAccount(), cpi))) {
/* 217 */         cal = cpi.cal;
/*     */       }
/* 219 */       if (cal == null) {
/* 220 */         throw ServiceException.FAILURE("SetCalendarItem could not build an iCalendar object", null);
/*     */       }
/* 222 */       boolean sentByMe = false;
/* 223 */       Invite iCalInv = (Invite)Invite.createFromCalendar(acct, pm.getFragment(acct.getLocale()), cal, sentByMe).get(0);
/*     */       
/* 225 */       if (inv == null) {
/* 226 */         inv = iCalInv;
/*     */       } else {
/* 228 */         inv.setDtStamp(iCalInv.getDTStamp());
/* 229 */         inv.setFragment(iCalInv.getFragment());
/*     */       }
/*     */     }
/* 232 */     inv.setPartStat(partStatStr);
/*     */     
/* 234 */     Mailbox.SetCalendarItemData sadata = new Mailbox.SetCalendarItemData();
/* 235 */     sadata.invite = inv;
/* 236 */     sadata.message = pm;
/* 237 */     return sadata;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static SetCalendarItemParseResult parseSetAppointmentRequest(Element request, ZimbraSoapContext zsc, OperationContext octxt, Folder folder, MailItem.Type type, boolean parseIds)
/*     */     throws ServiceException
/*     */   {
/* 250 */     Account acct = getRequestedAccount(zsc);
/* 251 */     Mailbox mbox = getRequestedMailbox(zsc);
/*     */     
/* 253 */     SetCalendarItemParseResult result = new SetCalendarItemParseResult();
/* 254 */     ArrayList<Mailbox.SetCalendarItemData> exceptions = new ArrayList();
/* 255 */     Invite defInv = null;
/*     */     
/*     */ 
/*     */ 
/* 259 */     Element e = request.getOptionalElement("default");
/* 260 */     if (e != null) {
/* 261 */       result.defaultInv = getSetCalendarItemData(zsc, octxt, acct, mbox, e, new SetCalendarItemInviteParser(false, false, folder, type));
/*     */       
/* 263 */       defInv = result.defaultInv.invite;
/*     */     }
/*     */     
/*     */ 
/*     */ 
/* 268 */     for (Element e : request.listElements("except")) {
/* 269 */       Mailbox.SetCalendarItemData exDat = getSetCalendarItemData(zsc, octxt, acct, mbox, e, new SetCalendarItemInviteParser(true, false, folder, type));
/*     */       
/* 271 */       exceptions.add(exDat);
/* 272 */       if (defInv == null) {
/* 273 */         defInv = exDat.invite;
/*     */       }
/*     */     }
/*     */     
/*     */ 
/* 278 */     for (Element e : request.listElements("cancel")) {
/* 279 */       Mailbox.SetCalendarItemData exDat = getSetCalendarItemData(zsc, octxt, acct, mbox, e, new SetCalendarItemInviteParser(true, true, folder, type));
/*     */       
/* 281 */       exceptions.add(exDat);
/* 282 */       if (defInv == null) {
/* 283 */         defInv = exDat.invite;
/*     */       }
/*     */     }
/*     */     
/* 287 */     if (exceptions.size() > 0) {
/* 288 */       result.exceptions = new Mailbox.SetCalendarItemData[exceptions.size()];
/* 289 */       exceptions.toArray(result.exceptions);
/*     */     }
/* 291 */     else if (result.defaultInv == null) {
/* 292 */       throw ServiceException.INVALID_REQUEST("No default/except/cancel specified", null);
/*     */     }
/*     */     
/*     */ 
/* 296 */     Element repliesElem = request.getOptionalElement("replies");
/* 297 */     if (repliesElem != null) {
/* 298 */       result.replies = CalendarUtils.parseReplyList(repliesElem, defInv.getTimeZoneMap());
/*     */     }
/* 300 */     result.isTodo = ((defInv != null) && (defInv.isTodo()));
/*     */     
/* 302 */     boolean noNextAlarm = request.getAttributeBool("noNextAlarm", false);
/* 303 */     if (noNextAlarm) {
/* 304 */       result.nextAlarm = -1L;
/*     */     } else {
/* 306 */       result.nextAlarm = request.getAttributeLong("nextAlarm", 0L);
/*     */     }
/* 308 */     return result;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/mail/SetCalendarItem.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */